package cn.myfirefly.moming.portal.dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;

import cn.myfirefly.moming.portal.entity.MMSystemAccess;
import cn.myfirefly.moming.portal.model.SystemAccessFilterDto;
import cn.myfirefly.core.BaseDao;
import cn.myfirefly.core.model.PageResultDto;
import cn.myfirefly.utils.CollectionUtil;
import cn.myfirefly.utils.FilterUtil;

@Repository
public class SystemAccessDao extends BaseDao<MMSystemAccess> {
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public PageResultDto<MMSystemAccess> getSystemAccessList(SystemAccessFilterDto dto) {
		Session session = getCurrentSession();
		PageResultDto resultDto = new PageResultDto();
		StringBuffer countSql = new StringBuffer("select count(t) from MMSystemAccess t where t.systemId !=1 ");
		StringBuffer querySql = new StringBuffer("from MMSystemAccess t where t.systemId !=1 ");
		List paramList = new ArrayList<>();
		FilterUtil.handleFilterSql(dto, "t", countSql, querySql, paramList);
		Query query = session.createQuery(countSql.toString());
		FilterUtil.setQueryParams(query, paramList);
		Number number = (Number) query.uniqueResult();
		resultDto.setTotal(number.intValue());
		if(number.intValue()>0){
			query = session.createQuery(querySql.toString());
			FilterUtil.setQueryParams(query, paramList);
			List<MMSystemAccess> list = query.setFirstResult((dto.getPageCurrent()-1) * dto.getPageSize())
					.setMaxResults(dto.getPageSize()).list();
			resultDto.setPageCurrent(dto.getPageCurrent());
			resultDto.setList(list);
		}
		return resultDto;
	}
	
	@SuppressWarnings("unchecked")
	public List<MMSystemAccess> getListBySysId(Integer sysId) {
		Session session = sessionFactory.getCurrentSession();
		StringBuffer querySql = new StringBuffer("from MMSystemAccess t where t.systemId=? ");
		Query query = session.createQuery(querySql.toString());
		query.setInteger(0, sysId);
		return query.list();
	}
	
	@SuppressWarnings("unchecked")
	public List<MMSystemAccess> getSystemAccessList() {
		Session session = sessionFactory.getCurrentSession();
		StringBuffer querySql = new StringBuffer("from MMSystemAccess t where t.systemAccessId != 1 and t.showStatus = '1' order by t.showIndex asc ");
		Query query = session.createQuery(querySql.toString());
		return query.list();
	}
	
	@SuppressWarnings("unchecked")
	public List<MMSystemAccess> getAllList() {
		Session session = sessionFactory.getCurrentSession();
		StringBuffer querySql = new StringBuffer("from MMSystemAccess t where 1 = 1 order by t.showIndex asc ");
		Query query = session.createQuery(querySql.toString());
		return query.list();
	}
	
	@SuppressWarnings("unchecked")
	public List<MMSystemAccess> getSystemAccessList(Character permissionType) {
		Session session = sessionFactory.getCurrentSession();
		StringBuffer querySql = new StringBuffer("from MMSystemAccess t where t.permissionType = 1 order by t.showIndex asc ");
		Query query = session.createQuery(querySql.toString());
		return query.list();
	}
	
	@SuppressWarnings("unchecked")
	public MMSystemAccess getBySysId(Integer systemId) {
		Session session = sessionFactory.getCurrentSession();
		StringBuffer querySql = new StringBuffer("from MMSystemAccess t where t.systemId=? ");
		Query query = session.createQuery(querySql.toString());
		query.setInteger(0, systemId);
		List<MMSystemAccess> list = query.list();
		if (CollectionUtil.isNotEmpty(list)) {
			return list.get(0);
		}
		return null;
	}
}
